package handler

import (
	"log"
	contact "git.oschina.net/fanbuchi/xgggh/users/proto/user/contact"
	oauthCli "git.oschina.net/fanbuchi/xgggh/oauth2/cli"
	"golang.org/x/net/context"
	"git.oschina.net/fanbuchi/xgggh/users/database"
)

type Contact struct{}

//micro query com.xgggh.srv.users Friend.Create '{"friendMobile":"15108280907","friendName":"zhangsan"}'

func (handler *Contact) Create(ctx context.Context, req *contact.CreateReq, rsp *contact.Response) error {
	revokeRsp,err:=oauthCli.Revoke(ctx)
	if err !=nil{
		return err
	}
	contacts := make([]contact.Contact,len(req.DataList))
	for  i:=0; i<len(req.DataList) ;i++  {
		contacts[i].AccountId=revokeRsp.Data.AccountId
	}
	_,err=database.Master().Insert(&contacts)
	return err
}

func (handler *Contact) Query(ctx context.Context, req *contact.QueryReq, rsp *contact.QueryRsp) error {
	log.Print("Received Example.Call request")
	//rsp.Id = 1111111
	return nil
}


func (handler *Contact) Update(ctx context.Context, req *contact.UpdateReq, rsp *contact.Response) error {
	log.Print("Received Example.Call request")
	//rsp.Data = 1111111
	return nil
}

func (handler *Contact) Delete(ctx context.Context, req *contact.DeleteReq, rsp *contact.Response) error {
	log.Print("Received Example.Call request")
	//rsp.Data = 1111111
	return nil
}

func (handler *Contact) QueryOne(ctx context.Context, req *contact.SearchReq, rsp *contact.SearchRsp) error {
	return handler.Search(ctx,req,rsp)
}
//micro query com.xgggh.srv.users Friend.Search '{"page":1,"perPage":1,"params":{"accountId":"211111"}}'

func (handler *Contact) Search(ctx context.Context, req *contact.SearchReq, rsp *contact.SearchRsp) error {
	revokeRsp,err:=oauthCli.Revoke(ctx)
	if err !=nil{
		return err
	}
	//page
	//limit,offset:=service.SearchPage(req.PageNumber,req.ResultPerPage)
	//support below data filter
	//log.Println(limit,offset)

	var conatcts []contact.Contact
	err = database.Master().Where("account_id = ?",
		revokeRsp.Data.AccountId).
		Limit(10, 0).
		Desc("id").
		Find(&conatcts)
	_result:=make([]*contact.Contact,len(conatcts))
	rsp.DataList=_result
	return  err
}



// Stream is a server side stream handler called via client.Stream or the generated client code
func (handler *Contact) Stream(ctx context.Context, req *contact.StreamingRequest, stream contact.Contacts_StreamStream) error {
	log.Printf("Received Example.Stream request with count: %d", req.Count)

	for i := 0; i < int(req.Count); i++ {
		log.Printf("Responding: %d", i)
		if err := stream.Send(&contact.StreamingResponse{
			Count: int64(i),
		}); err != nil {
			return err
		}
	}

	return nil
}

// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (handler *Contact) PingPong(ctx context.Context, stream contact.Contacts_PingPongStream) error {
	for {
		req, err := stream.Recv()
		if err != nil {
			return err
		}
		log.Printf("Got ping %v", req.Stroke)
		if err := stream.Send(&contact.Pong{Stroke: req.Stroke}); err != nil {
			return err
		}
	}
}
